//
//  GameplayLayer.m
//  LearningCocos
//
//  Created by abyssos on 2014. 2. 15..
//  Copyright (c) 2014년 ainhan. All rights reserved.
//

#import "GameplayLayer.h"

#import "Mallet.h"
#import "SpaceCargoShip.h"
#import "Health.h"
#import "EnemyRobot.h"
#import "PhaserBullet.h"

@implementation GameplayLayer

- (void)dealloc {
    [leftJoystick release];
    [jumpButton release];
    [attackButton release];
    [super dealloc];
}

- (void)initJoystickAndButtons {
    
    CGSize screenSize = [CCDirector sharedDirector].winSize;
    
    //
    CGRect joystickBaseDimensions = CGRectMake(0, 0, 128.0f, 128.0f);
    CGRect jumpButtonDimensions = CGRectMake(0, 0, 64.0f, 64.0f);
    CGRect attackButtonDimensions = CGRectMake(0, 0, 64.0f, 64.0f);
    //
    CGPoint joystickBasePosition;
    CGPoint jumpButtonPosition;
    CGPoint attackButtonPosition;

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//        joystickBasePosition = ccp(screenSize.width * 0.0625f, screenSize.height * 0.052f);
        joystickBasePosition = ccp(100.0f, 100.0f);
        jumpButtonPosition = ccp(screenSize.width * 0.946f, screenSize.height * 0.052f);
        attackButtonPosition = ccp(screenSize.width * 0.947f, screenSize.height * 0.169f);
    } else {
//        joystickBasePosition = ccp(screenSize.width * 0.07f, screenSize.height * 0.11f);
//        jumpButtonPosition = ccp(screenSize.width * 0.93f, screenSize.height * 0.11f);
//        attackButtonPosition = ccp(screenSize.width * 0.93f, screenSize.height * 0.35f);
        CCLOG(@"joystick size : %f * %f", joystickBaseDimensions.size.width, joystickBaseDimensions.size.height);
        joystickBasePosition = ccp(64.0f, 264.0f);
        jumpButtonPosition = ccp(screenSize.width - 128.0f, 264.0f);
        attackButtonPosition = ccp(screenSize.width, 264.0f);
    }
    
    SneakyJoystickSkinnedBase *joystickBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
    joystickBase.position = joystickBasePosition;
    joystickBase.backgroundSprite = [CCSprite spriteWithFile:@"dpadDown.png"];
    joystickBase.thumbSprite = [CCSprite spriteWithFile:@"joystickDown.png"];
    joystickBase.joystick = [[SneakyJoystick alloc] initWithRect:joystickBaseDimensions];
    leftJoystick = [joystickBase.joystick retain];
    [self addChild:joystickBase];
    
    SneakyButtonSkinnedBase *jumpButtonBase = [[[SneakyButtonSkinnedBase alloc] init] autorelease];
    jumpButtonBase.position = jumpButtonPosition;
    jumpButtonBase.defaultSprite = [CCSprite spriteWithFile:@"jumpUp.png"];
    jumpButtonBase.activatedSprite = [CCSprite spriteWithFile:@"jumpDown.png"];
    jumpButtonBase.pressSprite = [CCSprite spriteWithFile:@"jumpDown.png"];
    jumpButtonBase.button = [[SneakyButton alloc] initWithRect:jumpButtonDimensions];
    jumpButton = [jumpButtonBase.button retain];
    jumpButton.isToggleable = NO;
    [self addChild:jumpButtonBase];
    
    SneakyButtonSkinnedBase *attackButtonBase = [[[SneakyButtonSkinnedBase alloc] init] autorelease];
    attackButtonBase.position = attackButtonPosition;
    attackButtonBase.defaultSprite = [CCSprite spriteWithFile:@"handUp.png"];
    attackButtonBase.activatedSprite = [CCSprite spriteWithFile:@"handDown.png"];
    attackButtonBase.pressSprite = [CCSprite spriteWithFile:@"handDown.png"];
    attackButtonBase.button = [[SneakyButton alloc] initWithRect:attackButtonDimensions];
    attackButton = [attackButtonBase.button retain];
    attackButton.isToggleable = NO;
    [self addChild:attackButtonBase];
    
}


//- (void)applyJoystick:(SneakyJoystick *)aJoystick toNode:(CCNode *)tempNode forTimeDelta:(float)deltaTime {
//    
//    CGSize screenSize = [CCDirector sharedDirector].winSize;
//    
//    CGPoint scaledVelocity = ccpMult(aJoystick.velocity, 1024.0f);
//    
//    float newX = tempNode.position.x + scaledVelocity.x * deltaTime;
//    float newY = tempNode.position.y + scaledVelocity.y * deltaTime;
//    
//    if(newX < 0)
//        newX = 0.0f;
//    else if(newX > screenSize.width)
//        newX = screenSize.width;
//
//    if(newY < 0)
//        newY = 0.0f;
//    else if(newY > screenSize.height)
//        newY = screenSize.height;
//    
//    CGPoint newPosition = ccp(newX, newY);
//    
//    [tempNode setPosition:newPosition];
//    
//    if(jumpButton.active == YES) {
//        CCLOG(@"Jump button is pressed.");
//    }
//
//    if(attackButton.active == YES) {
//        CCLOG(@"Attack button is pressed.");
//    }
//}

#pragma mark -
#pragma mark Update Method
- (void)update:(ccTime)deltaTime {
    
    CCArray *listOfGameObjects = [sceneSpriteBatchNode children];
    for(GameCharacter *tempChar in listOfGameObjects) {
        
        [tempChar updateStateWithDeltaTime:deltaTime andListOfGameObjects:listOfGameObjects];
        
    }
    
}

#pragma mark -
- (void)createObjectOfType:(GameObjectType)objectType withHealth:(int)initialHealth atLocation:(CGPoint)spawnLocation withZValue:(int)ZValue {

    if(objectType == kEnemyTypeRadarDish) {
        CCLOG(@"Creating the Radar Enemy");
        RadarDish *radarDish = [[RadarDish alloc] initWithSpriteFrameName:@"radar_1.png"];
        [radarDish setCharacterHealth:initialHealth];
        [radarDish setPosition:spawnLocation];
        [sceneSpriteBatchNode addChild:radarDish z:ZValue tag:kRadarDishTagValue];
        
        [radarDish release];
    } else if (objectType == kEnemyTypeAlienRobot) {
        CCLOG(@"Creating the Alien Robot");
        EnemyRobot *enemyRobot = [[EnemyRobot alloc] initWithSpriteFrameName:@"an1_anim1.png"];
        [enemyRobot setCharacterHealth:initialHealth];
        [enemyRobot setPosition:spawnLocation];
        [enemyRobot changeState:kStateSpawning];
        [sceneSpriteBatchNode addChild:enemyRobot z:ZValue];
        [enemyRobot setDelegate:self];
        [enemyRobot release];
    } else if (objectType == kEnemyTypeSpaceCargoShip) {
        CCLOG(@"Creating the Cargo Ship Enemy");
        SpaceCargoShip *spaceCargoShip = [[SpaceCargoShip alloc] initWithSpriteFrameName:@"ship_2.png"];
        [spaceCargoShip setDelegate:self];
        [spaceCargoShip setPosition:spawnLocation];
        [sceneSpriteBatchNode addChild:spaceCargoShip z:ZValue];
        [spaceCargoShip release];
    } else if (objectType == kPowerUpTypeMallet) {
        CCLOG(@"GamePlayerLayer -> Creating mallet powerup");
        Mallet *mallet = [[Mallet alloc] initWithSpriteFrameName:@"mallet_1.png"];
        [mallet setPosition:spawnLocation];
        [sceneSpriteBatchNode addChild:mallet];
        [mallet release];
    } else if (objectType == kPowerUpTypeHealth) {
        CCLOG(@"GameplayLayer -> Creating Health Powerup");
        Health *health = [[Health alloc] initWithSpriteFrameName:@"sandwich_1.png"];
        [health setPosition:spawnLocation];
        [sceneSpriteBatchNode addChild:health];
        [health release];
    }
}

- (void)createPhaserWithDirection:(PhaserDirection)PhaserDirection andPosition:(CGPoint)spawnPosition {
    CCLOG(@"CREATE PHASER position : %f, %f", spawnPosition.x, spawnPosition.y);
    PhaserBullet *phaserBullet = [[PhaserBullet alloc] initWithSpriteFrameName:@"beam_1.png"];
    
    CCLOG(@"CREATE PHASER -> %@", phaserBullet);
    
    
    [phaserBullet setPosition:spawnPosition];
    [phaserBullet setMyDirection:PhaserDirection];
    
    CCLOG(@"PHASER kStateSpawning : %d", kStateSpawning);
    
    [phaserBullet setCharacterState:kStateSpawning];
    [sceneSpriteBatchNode addChild:phaserBullet];
    [phaserBullet release];

    return;
}

- (void)addEnemy {
    CGSize screenSize = [CCDirector sharedDirector].winSize;
    RadarDish *radarDish = (RadarDish *)[sceneSpriteBatchNode getChildByTag:kRadarDishTagValue];
    if (radarDish != nil) {
        if ([radarDish characterState] != kStateDead) {
            [self createObjectOfType:kEnemyTypeAlienRobot withHealth:100 atLocation:ccp(screenSize.width * 0.195f, screenSize.height * 0.1432f) withZValue:2];
        } else {
            [self unschedule:@selector(addEnemy)];
        }
    }
}


- (id)init {
    
    CCLOG(@"## [GameplayLayer] init.......");
    
    self = [super init];
    if(self == nil) {
        return nil;
    }
    
    CGSize screenSize = [CCDirector sharedDirector].winSize;
    CCLOG(@"## [GameplayLayer] screenSize : %f * %f", screenSize.width, screenSize.height);

    // enable touchs
    self.touchEnabled = YES;
    
    srandom(time(NULL));

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        CCLOG(@"ipad...........");
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"scene1atlas.plist"];
        sceneSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"scene1atlas.png"];
    } else {
        CCLOG(@"iphone...........");
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"scene1atlasiPhone.plist"];
        sceneSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"scene1atlasiPhone.png"];
    }
    
    
    [self addChild:sceneSpriteBatchNode z:0];
    [self initJoystickAndButtons];
    
    CCLOG(@"## [GameplayLayer] create Viking...");
    Viking *viking = [[Viking alloc] initWithSpriteFrameName:@"sv_anim_1.png"];
    
    [viking setJoystick:leftJoystick];
    [viking setJumpButton:jumpButton];
    [viking setAttackButton:attackButton];
    
    [viking setPosition:ccp(screenSize.width * 0.3f, screenSize.height * 0.14f)];
    [viking setCharacterHealth:100];
    
    [sceneSpriteBatchNode addChild:viking z:kVikingSpriteZValue tag:kVikingSpriteTagValue];
    
    
    // create Radar
    [self createObjectOfType:kEnemyTypeRadarDish withHealth:100 atLocation:ccp(screenSize.width * 0.878f, screenSize.height * 0.13f) withZValue:10];
    
    [self scheduleUpdate];
    
    // create EnemyRobot
    
    [self schedule:@selector(addEnemy) interval:10.0f];
    [self createObjectOfType:kEnemyTypeSpaceCargoShip withHealth:0 atLocation:ccp(screenSize.width * -0.5f, screenSize.height * 0.74f) withZValue:50];
    //[self createObjectOfType:kEnemyTypeSpaceCargoShip withHealth:0 atLocation:ccp(0.0f, 0.0f) withZValue:50];
    
    return self;
}

@end
